package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.mapper.CollectMapper;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.query.CollecQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author pengq
 */
@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper contentMapper;
    @Autowired
    private CollectMapper collectMapper;

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail) {
        // 当序号有值时，进行排序处理
        if (strategyDetail.getSequence() == null) {
            // 查询出当前攻略下文章的最大序号
            int max = strategyDetailMapper.selectMaxSequence(strategyDetail.getCatalog().getId());
            // 设置序号最大值+1
            strategyDetail.setSequence(max + 1);
        }

        // 判断状态是否为发布
        if (strategyDetail.getState() == StrategyDetail.STATE_RELEASE) {
            // 是发布，添加发布时间
            strategyDetail.setReleaseTime(new Date());
        }
        if (strategyDetail.getId() != null) {
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            // 关联文章对象
            StrategyContent content = strategyDetail.getContent();
            // 修改内容
            contentMapper.updateByPrimaryKey(content);
        } else {
            // 保存创建时间
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);
            // 关联文章对象
            StrategyContent content = strategyDetail.getContent();
            // 将id设置到内容对象中
            content.setId(strategyDetail.getId());
            // 新增内容
            contentMapper.insert(content);

        }
    }

    @Override
    public StrategyContent getContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public StrategyDetail getById(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<StrategyDetail> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), "sd.sequence asc");
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<StrategyDetail> listContentByKeyword(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyDetail> list = strategyDetailMapper.selectContentByKeyword(qo);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<StrategyDetail> selectCollectDetaxil(Collect collect, CollecQueryObject qo) {
        //设置当前用户
        collect.setUser(UserContext.getCurrentUser());
        //查询出所有收藏文章的id
        List<Integer> collectId = collectMapper.selectCollectId(collect);
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyDetail> details = strategyDetailMapper.selectByCollectId(collectId);
        return new PageInfo<>(details);
    }

}
